<?php
$sync_bind_exists = $_FANWE['cookie']['sync_bind_exists'];
$result = array();
if(empty($sync_bind_exists))
{
	$result['status'] = 0;
	outputJson($result);
}

$sync_bind_exists = unserialize(authcode($sync_bind_exists, 'DECODE'));
$is_bind = $_FANWE['request']['is_bind'];

if($is_bind)
{
	$avatar = '';
	switch($sync_bind_exists['bind_type'])
	{
		case 'sina':
			FDB::query("update ".FDB::table("user")." set sina_app_key = '',sina_app_secret = '',sina_syn_weibo = 1,sina_syn_topic=1,sina_id='' where sina_id = '".$sync_bind_exists['sina_id']."'");
			FDB::query("update ".FDB::table("user")." set sina_app_key = '".$sync_bind_exists['sina_app_key']."',sina_app_secret = '".$sync_bind_exists['sina_app_secret']."',sina_id='".$sync_bind_exists['sina_id']."' where uid = ".$_FANWE['uid']);
			$avatar = str_replace('/50/','/180/',$sync_bind_exists['profile_image_url']);
		break;
		
		case 'qq':
			FDB::query("update ".FDB::table("user")." set qq_id='' where qq_id = '".$sync_bind_exists['qq_id']."'");
			FDB::query("update ".FDB::table("user")." set qq_id='".$sync_bind_exists['qq_id']."' where uid = ".$_FANWE['uid']);
			$avatar = $sync_bind_exists['figureurl_2'];
		break;
		
		case 'taobao':
			FDB::query("update ".FDB::table("user")." set taobao_id='' where qq_id = '".$sync_bind_exists['taobao_id']."'");
			FDB::query("update ".FDB::table("user")." set taobao_id='".$sync_bind_exists['taobao_id']."' where uid = ".$_FANWE['uid']);
		break;
		
		case 'tqq':
			FDB::query("update ".FDB::table("user")." set tqq_app_key = '',tqq_app_secret = '',tqq_syn_weibo = 1,tqq_syn_topic=1,tqq_id='' where tqq_id = '".$sync_bind_exists['tqq_id']."'");
			FDB::query("update ".FDB::table("user")." set tqq_app_key = '".$sync_bind_exists['tqq_app_key']."',tqq_app_secret = '".$sync_bind_exists['tqq_app_secret']."',tqq_id='".$sync_bind_exists['tqq_id']."' where uid = ".$_FANWE['uid']);
			if(!empty($sync_bind_exists['head']))
				$avatar = $sync_bind_exists['head'].'/180';
		break;
	}
	
	if(!empty($avatar) && !FS('User')->getIsAvatar($_FANWE['uid']))
	{
		$img = copyFile($avatar);
		if($img !== false)
			FS('User')->saveAvatar($_FANWE['uid'],$img['path']);
	}
	$result['status'] = 1;
}
else
{
	$result['status'] = 0;
}

fSetCookie('sync_bind_exists','');
outputJson($result);
?>